package org.gjy.m8.concurrent.cf;

import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-09 14:06:31
 */
public class Cf1Test {

    public static void main(String[] args) {


    }

    private static void executor() {
        int processors = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(processors, processors * 10, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "10";
        }, executor);
    }

    private static void thenCompose() {
        CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> 10)
                .thenCompose(r -> CompletableFuture.supplyAsync(() -> r * 20));
        try {
            Integer i = cf.get();
            System.out.println(i);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void handle() {
        CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> 0)
                .thenApply(t -> 12 / t)
                .handle((r, e) -> {
                    if (e != null) {
                        System.out.println(e.getMessage());
                        return -1;
                    } else {
                        return r;
                    }
                });
        try {
            Integer i = cf.get();
            System.out.println(i);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void exceptionally() {
        CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> 0)
                .thenApply(t -> 12 / t)
                .exceptionally(e -> {
                    System.out.println(e.getMessage());
                    return -1;
                });
        try {
            Integer i = cf.get();
            System.out.println(i);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void anyOf() {
        CompletableFuture<Object> cf = CompletableFuture.anyOf(
                CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(3);
                        System.out.println("hello");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }),
                CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(5);
                        System.out.println("world");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }),
                CompletableFuture.runAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                        System.out.println("cf");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                })
        );
        cf.join();
    }

    private static void allOf() {
        CompletableFuture<Void> cf = CompletableFuture.allOf(
                CompletableFuture.runAsync(() -> System.out.println("hello")),
                CompletableFuture.runAsync(() -> System.out.println("world")),
                CompletableFuture.runAsync(() -> System.out.println("cf"))
        );
        cf.join();
    }

    private static void thenCombine() {
        CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> 10);
        CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> 20);
        CompletableFuture<Integer> ret = f1.thenCombine(f2, Integer::sum);
        ret.thenAccept(result -> System.out.println("合并后的结果: " + result));
    }

    private static void thenRun() {
        CompletableFuture.supplyAsync(() -> 10)
                .thenRun(() -> System.out.println(Thread.currentThread().getName()));
    }

    private static void thenAccept() {
        CompletableFuture.supplyAsync(() -> 10)
                .thenAccept(ta -> System.out.println(ta * 30));
    }

    // 链式调用
    private static void thenApply() {
        CompletableFuture<String[]> cf = CompletableFuture.supplyAsync(() -> {
                    System.out.println(Thread.currentThread().getName());
                    return "t-t";
                })
                .thenApplyAsync(t -> t.split("-"));
//                .thenApply(r -> r.split("-"));
        try {
            String[] strings = cf.get();
            System.out.println(Arrays.toString(strings));
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void supplyAsync() {
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "HG";
        });
        try {
            String s = cf.get();
            System.out.println(Thread.currentThread().getName() + "-" + s);
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private static void runAsync() {
        CompletableFuture.runAsync(() -> {
            System.out.println("s");
        });
    }
}
